var app = getApp();
// pages/order/downline.js
Page({
  data:{
    itemData:{},
    userId:0,
    paytype:'weixin',//0线下1微信
    remark:'',
    cartId:0,
    addrId:0,//收货地址//测试--
    btnDisabled:false,
    productData:[],
    address:{},
    total:0,
    vprice:0,
    vid:0,
    addemt:1,
    vou: [],
    goodsurl: app.d.hostUrl + app.d.goodsurl,
    business:'',
    peisongfei:0,
    invite:0,
  },
  onLoad: function (options) {
    if(options.invite > 0){
      app.globalData.invite = options.invite;
      this.setData({
        invite: options.invite,
      });
    }
    var uid = app.d.userId;
    this.setData({
      cartId: options.cartId,
      userId: uid
    });
    this.loadProductDetail();



    
  },
  loadProductDetail:function(){
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/payment_cart.html',
      method:'post',
      data: {
        cart_id: that.data.cartId,
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
      },
      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //that.initProductData(res.data);
        var adds = res.data.data.consignee_list;
        //var ss = JSON.parse(res.data.data.business);
        /**if (adds){
          var addrId = adds.id;
          that.setData({
            address: adds,
            addrId: addrId,
            //business: ss
          });
        }else{
          wx.showToast({
            title: "请添加收货地址",
            duration: 3000
          });
        }**/
        /**var totalm = 0.00;
        var peisongfei = 0.00;
        if (res.data.data.total > ss.mianyou){
          totalm = res.data.data.total;
        }else{
          totalm = res.data.data.total + ss.peisongfei;
          peisongfei = ss.peisongfei;
        }**/console.log(res.data.data)
        that.setData({
          addemt: res.data.data.addemt,
          productData: res.data.data.cart,
          total: res.data.data.total,
          vprice: res.data.data.total,
          vou: res.data.data.vou,
          peisongfei: res.data.data.peisongfei
        });
        //endInitData
      },
    });
  },

  remarkInput:function(e){
    this.setData({
      remark: e.detail.value,
    })
  },

 //选择优惠券
  getvou: function (e) {
    var vid = e.currentTarget.dataset.id;
    var price = e.currentTarget.dataset.price;
    var zprice = this.data.vprice;
    var count_price = parseFloat(zprice) - parseFloat(price) 
    var cprice = (count_price > 0) ? count_price.toFixed(2): 0.01;
    this.setData({
      total: cprice,
      vid: vid
    })
  }, 

//微信支付
  createProductOrderByWX:function(e){
    this.setData({
      paytype: 'weixin',
    });

    this.createProductOrder();
  },

  //线下支付
  createProductOrderByXX:function(e){
    this.setData({
      paytype: 'cash',
    });
    wx.showToast({
      title: "线下支付开通中，敬请期待!",
      duration: 3000
    });
    return false;
    this.createProductOrder();
  },


  //线下支付
  createProductOrderCompany: function (e) {
    this.setData({
      paytype: 'company',
    });
    this.createProductOrder();
  },
  //确认订单
  createProductOrder:function(){
    this.setData({
      btnDisabled:false,
    })
    //创建订单
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/payment.html',
      method:'post',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        cart_id: that.data.cartId,
        paytype:that.data.paytype,
        aid: that.data.addrId,//地址的id
        remark: that.data.remark,//用户备注
        price: that.data.total,//总价
        vid: that.data.vid,//优惠券ID
        invite: app.globalData.invite,
      },
      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var data = res.data.data;
        if (res.data.ret == 0){
          //创建订单成功
          if (data.payment_method == 'cash') {
            wx.showToast({
              title: "请自行联系商家进行发货!",
              duration: 3000
            });
            return false;
          }
          if (data.payment_method == 1) {
            wx.showToast({
              title: "下单成功",
              duration: 2000
            });
            setTimeout(function () {
              wx.navigateTo({
                url: '../user/dingdan?currentTab=2&otype=ship',
              });
            }, 2500);
            return false;
          }
          if (data.payment_method == 0){
            //微信支付
            that.wxpay(data);
          }
        }else{
          wx.showToast({
            title: res.data.msg,
            duration:2000
          });
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！err:createProductOrder',
          duration: 2000
        });
      }
    });
  },
  
  //调起微信支付
  wxpay: function(order){
    var that = this;
      wx.request({
        url: app.d.hostUrl + 'wapi/wxpay.html',
        data: {
          order_id: order.order_id,
          openid: app.globalData.userInfo.openid,
          seesionid: app.globalData.userInfo.sessionId,
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'Content-Type':  'application/x-www-form-urlencoded'
        }, // 设置请求的 header
        success: function(res){
          if(res.data.status == 0){
            var order = res.data.data;
            wx.requestPayment({
              timeStamp: order.timeStamp,
              nonceStr: order.nonceStr,
              package: order.package,
              signType: 'MD5',
              paySign: order.paySign,
              success: function(res){
                wx.showToast({
                  title:"支付成功!",
                  duration:2000,
                });
                setTimeout(function(){
                  wx.navigateTo({
                    url: '../user/dingdan?currentTab=3&otype=sign',
                  });
                },2500);
              },
              fail: function(res) {
                wx.showToast({
                  title:res,
                  duration:3000
                })
              }
            })
          }else{
            wx.showToast({
              title: res.data.err,
              duration: 2000
            });
          }
        },
        fail: function() {
          // fail
          wx.showToast({
            title: '网络异常！err:wxpay',
            duration: 2000
          });
        }
      })
  },
  onShareAppMessage: function () {
    return {
      title: '极好生活商城',
      path: '/pages/index/index?scene=invite%3D' + app.globalData.user_id,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          duration: 2000
        });
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },

});